package com.gujia.service;

import com.gujia.dao.LoginTicketDao;
import com.gujia.dao.UserDao;
import com.gujia.model.LoginTicket;
import com.gujia.model.User;
import com.gujia.util.AnswerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by GuJia on 2017/6/17.
 */
@Service
public class UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginTicketDao loginTicketDao;

    public User getUser(int id){
        return userDao.selectById(id);
    }

    public Map<String,String> register(String name,String password){
        Map<String,String> map = new HashMap();
        if(StringUtils.isBlank(name)){

            map.put("msg","User name is null!");
            return map;
        }

        if(StringUtils.isBlank(password)){
            map.put("mag","Password is null!");
            return map;
        }

        User user = userDao.selectByName(name);;

        if(null!=user){
            map.put("msg","User name has exist!");
            return map;
        }
        user = new User();
        user.setName(name);
        user.setSalt(UUID.randomUUID().toString().substring(0,5));
        user.setHeadUrl(String.format("http://images.newcoder.com/head/%dt.png",new Random().nextInt(1000)));
        user.setPassword(AnswerUtil.MD5(password+user.getSalt()));
        userDao.addUser(user);


        String ticket = addTicket(user.getId());

        map.put("ticket",ticket);

        return map;
    }


    public Map<String,Object> login(String name,String password){

        Map<String,Object> map = new HashMap();
        if(StringUtils.isBlank(name)){

            map.put("msg","User name is null!");
            return map;
        }

        if(StringUtils.isBlank(password)){
            map.put("mag","Password is null!");
            return map;
        }

        User user = new User();

        user = userDao.selectByName(name);

        if(null==user){
            map.put("msg","User name has not exist!");
            return map;
        }

        if(!AnswerUtil.MD5(password+user.getSalt()).equals(user.getPassword())){
            map.put("msg","Password error!");
            return map;
        }

        String ticket = addTicket(user.getId());
        map.put("userId",user.getId());
        map.put("ticket",ticket);

        return map;
    }

    public String addTicket(int usderId){
        LoginTicket loginTicket = new LoginTicket();

        loginTicket.setUserId(usderId);

        Date expired = new Date();
        expired.setTime(3600*24*100+expired.getTime());
        loginTicket.setExpired(expired);
        loginTicket.setStatus("0");
        loginTicket.setTicket(UUID.randomUUID().toString().replace("-",""));

        loginTicketDao.addTicket(loginTicket);

        return  loginTicket.getTicket();
    }

    public void logout(String ticket){
        loginTicketDao.updatetatus(ticket,"1");
    }



    public User getUserByName(String userName){
       return userDao.selectByName(userName);
    }

}
